<template>
  <view>
    <my-search @click="gotoSearch()"></my-search>
    <view class="scroll-view-container">
      <!-- 左侧滑动区 -->
      <scroll-view :style="'height:'+vh + 'px'" class="left-scroll-view" scroll-y="true">
        <view @click="activeChange(idx)" :class="idx!==active?'left-item':'left-item active'"
          v-for="(item, idx) in cateList" :key="item.cat_id">
          {{item.cat_name}}
        </view>

      </scroll-view>
      <!-- 右侧滑动区 -->
      <scroll-view :scroll-top="scrollTop" :style="'height:'+ vh + 'px'" class="right-scroll-view" scroll-y="true">
        <!-- 二级分类 -->
        <view class="cate-lv2" v-for="(item,index) in cateLevel2" :key="index">
          <!-- 二级分类标题 -->
          <view class="cate-lv2-title">
            /{{item.cat_name}}/
          </view>
          <!-- 三级分类 -->
          <view class="cate-lv3-list">
            <view @click="gotoGoodsList(item3)" v-for="(item3, idx3) in item.children" :key="idx3"
              class="cate-lv3-item">
              <image :src="item3.cat_icon.replace('dev','web')" mode=""></image>
              <text>{{item3.cat_name}}</text>
            </view>

          </view>

        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  // 导入自己封装的 mixin 模块
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    // 将 badgeMix 混入到当前的页面中进行使用
    mixins: [badgeMix],
    data() {
      return {
        vh: 0,
        cateList: [],
        cateLevel2: [],
        // 当前激活项
        active: 0,
        // 滚动距离顶部距离
        scrollTop: 0
      };
    },
    methods: {
      async getCateList() {
        const { data: res } = await this.$axios.get('/categories')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        this.cateLevel2 = res.message[0].children
      },
      activeChange(idx) {
        this.active = idx
        this.cateLevel2 = this.cateList[idx].children
        //重置滚动区域 重复赋值一样不会生效
        this.scrollTop = this.scrollTop ? 0 : 1
      },
      gotoGoodsList(item) {
        uni.navigateTo({
          url: "/subpkg/goods_list/goods_list?cid=" + item.cat_id
        })
      },
      gotoSearch() {
        uni.navigateTo({
          url: "/subpkg/search/search"
        })
      }
    },
    onLoad() {
      this.getCateList()
      // 获取可用高度
      const sysInfo = uni.getSystemInfoSync()
      this.vh = sysInfo.windowHeight - 50
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .left-scroll-view {
      width: 120px;

      .left-item {
        height: 120rpx;
        text-align: center;
        line-height: 120rpx;
        background-color: #f7f7f7;
        font-size: 24rpx;

        &.active {
          position: relative;
          background-color: #ffffff;

          &::before {
            content: '';
            position: absolute;
            height: 70rpx;
            width: 5rpx;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
            background-color: #c00000;
          }


        }
      }

    }


    .right-scroll-view {
      .cate-lv2-title {
        text-align: center;
        font-weight: 700;
        padding: 30rpx 0;
        font-size: 24rpx;
      }

      .cate-lv3-list {
        display: flex;
        flex-wrap: wrap;

        .cate-lv3-item {
          width: 33.33%;
          display: flex;
          flex-direction: column;
          align-items: center;
          margin-bottom: 20rpx;

          image {
            width: 120rpx;
            height: 120rpx;
          }

          text {
            font-size: 12px;
          }
        }
      }
    }
  }
</style>
